package com.demo.wormhole.component.redis.serializer;

import com.demo.wormhole.core.configuration.WormholeIsolationConfiguration;
import com.demo.wormhole.core.context.WormholeContext;
import com.demo.wormhole.core.context.WormholeContextHolder;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.lang.Nullable;

import javax.annotation.Resource;

public class KeyStringRedisSerializer extends StringRedisSerializer {
    
    @Resource
    private WormholeIsolationConfiguration isolationConfiguration;
    
    @Override
    public byte[] serialize(@Nullable String redisKey) {
        WormholeContext wormholeContext = WormholeContextHolder.getContext();
        if (null != wormholeContext) {
            redisKey = isolationConfiguration.generateIsolationKey(redisKey);
        }
        return super.serialize(redisKey);
    }
}
